function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r !== null) return unescape(r[2]);
    return null;
}

var getById = function (id) {
    if (!id) {
        return;
    }
    $.post(baseUrl + 'admin/role/get', {id: id}, function (result) {
        var r = result.value;
        $("input[name='id']").val(r.id);
        $("input[name='roleName']").val(r.roleName);
        $("input[name='roleDesc']").val(r.roleDesc);
        $('#adminValues').val(r.adminIds);
        $("#menuIds").val(r.menuIds);
    })
}

var selectAdmin = function () {
    $.get(baseUrl + 'admin/sysAdmin/list', {}, function (result) {
        if (result.data === null) {
            return;
        }
        var adminValue = $('#adminValues').val();
        var optHtml;
        result.data.forEach(function (t) {
            var isS = adminValue.indexOf(t.id) > -1 ? 'selected' : '';
            optHtml += '<option value="' + t.id + '" ' + isS + '>' + t.account + '</option>';
        })
        $(".chosen-select").append(optHtml);
        $(".chosen-select").trigger('chosen:updated');
    });
}

function goBack() {
    location.href = baseUrl + 'admin/system/role-list.html';
}

var setting = {
    check: {
        enable: true,
        chkStyle: "checkbox",
        chkboxType: {"Y": "ps", "N": "ps"}
    },
    data: {
        simpleData: {
            enable: true,
            pIdKey: "parentId"
        }
    },
    callback: {
        onCheck: function (event, treeId, treeNode, clickFlag) {
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
            var nodes = treeObj.getCheckedNodes(true);
            var v = "";
            for (var i = 0; i < nodes.length; i++) {
                v += nodes[i].id + ",";
            }
            $("#menuIds").val(v); //获取选中节点的值
        }
    }
};

var getTreeValue = function () {
    var zTree;
    $.post(baseUrl + 'admin/sysMenu/listAll', {}, function (data) {
        var zNodes = data.value;
        var menuList = $("#menuIds").val();

        for (var i = 0; i < zNodes.length; i++) {
            if (menuList.indexOf(zNodes[i].id) > -1) {
                zNodes[i].checked = true;
            }
        }
        zTree = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        zTree.expandAll(true);
    })
}


$(document).ready(function () {
    $(".chosen-select").chosen();

    var id = getQueryString('id');
    getById(id);
    setTimeout(function () {
        selectAdmin();
        getTreeValue();
    }, 100);

    $.fn.zTree.init($("#treeDemo"), setting);


    $("#btnSave").click(function () {
        var str = $("#myForm").serializeArray();
        $.ajax({
            url: baseUrl + 'admin/role/save',
            type: 'post',
            data: str,
            success: function (data) {
                if (data.code === 0) {
                    location.href = baseUrl + 'admin/system/role-list.html';
                } else {
                    layer.msg(data.message);
                }
            },
            error: function () {
                layer.msg("请求出错")
            }
        });
    });
})